///////////////////////////////////////////////////
/*
Kevin DeLuca
Replication data for: "Editor's Choice: Measuring Candidate Quality Using Local Newspaper Endorsements", Journal of Politics, 2025
DOI: 
Appendix Results
*/
///////////////////////////////////////////////////

clear
set more off
graph drop _all

*set directory here
global mainPath = ""

cd "$mainPath"


**packages required for replication**
*ssc install reghdfe //both needed for estimation
*ssc install ftools //both needed for estimation
*ssc install outreg2 //needed for tables
*ssc install gtools //needed to run binscatter2, for figures
*net install binscatter2, from("https://raw.githubusercontent.com/mdroste/stata-binscatter2/master/") //needed for some figures

*scheme for figures
set scheme stmono1


**************************
*Appendix A
**************************

///////////////////////////////////////////////////
**********
*Appendix Figure A1
**********
use "$mainPath/all_endorsements_JOP.dta", clear
gen n = 1
collapse (sum) n, by(year)
graph bar n, over(year, label(angle(50))) ytitle("Total Number of Endorsements") //title("Local Newspaper Endorsement Data, 1950-2020")
graph export "$mainPath/figureA1.pdf", width(5.5) replace
///////////////////////////////////////////////////



*** Note: For appendix figure A2, see the R code "FigureA2.R" *** 



///////////////////////////////////////////////////
**********
*Appendix Figure A3
**********
use "$mainPath/all_endorsements_JOP.dta", clear
duplicates drop election_g, force
collapse (mean) endorseD, by(year)
graph twoway scatter endorseD year, c(l) ylab(0(0.2)1) xlab(1950(4)2020, angle(45)) yline(0.5, lcolor(black) lpattern(dash)) ytitle("Share of Democratic Endorsements") xtitle("Year") 
graph export "$mainPath/figureA3.pdf", width(5.5) replace
///////////////////////////////////////////////////



///////////////////////////////////////////////////
**********
*Appendix Table A1
**********
use "$mainPath/all_endorsements_JOP.dta", clear

*estimate quality
bysort member: egen temp = count(member)
drop if temp<10
drop temp
reghdfe endorseD, absorb(member member#c.time election_g, savefe)
keep if e(sample)
rename __hdfe1__ fe_partisanship
replace fe_partisanship = fe_partisanship+_b[_cons]
replace fe_partisanship = fe_partisanship+(time*__hdfe2__Slope1)
rename __hdfe2__Slope1 fe_partisantrend
rename __hdfe3__ fe_election

*fe_p
egen tag = tag(member year)
tab tag
summ fe_partisanship if tag==1, d
replace fe_partisanship = fe_partisanship-0.5 //relative to a neutral paper
summ fe_partisanship if tag==1, d
replace fe_partisanship = fe_partisanship/r(sd) //unit std dev
drop tag
*fe_e
egen tag = tag(election_g)
tab tag
summ fe_election if tag==1, d
replace fe_election = fe_election/r(sd)
drop tag

*full sample of endorsements and papers, with estimates
gen n_endorsements=1
collapse (sum) n_endorsements (first) state_newspaper newspaper (mean) endorseD fe_partisanship fe_partisantrend, by(member)
replace fe_partisantrend = fe_partisantrend*10 //by decade
replace newspaper = subinstr(newspaper,"&","AND",.)
replace newspaper = strproper(newspaper)
replace newspaper = subinstr(newspaper,"And","and",.)

sort state_newspaper newspaper

local N = _N
quietly {

  log using "$mainPath/tableA1.tex", text replace
  
  noisily display "\begin{longtable}{l @{ \extracolsep{\fill}} *{1}{l} @{\extracolsep{\fill}} *{5}{c} }"
  noisily display "\caption{Sample of Newspaper Endorsements and Partisanship}\label{tab:sampletable_dissch1}\\"
  noisily display "\hline"
  noisily display "State & Newspaper Name & Dem Share & Bias & Trend & N \\"
  noisily display "\midrule"
  noisily display "\endfirsthead"
  noisily display "\endhead"

  noisily display "\midrule[\heavyrulewidth]"
  noisily display "\multicolumn{6}{r}{\textit{continued}}\\"
  noisily display "\endfoot  "

  noisily display "\midrule[\heavyrulewidth]"
  noisily display "\insertTableNotes  "
  noisily display "\endlastfoot"

     forvalues i = 1(1)`N' {
      noisily display state_newspaper[`i'] " & " newspaper[`i'] " & " %5.2f endorseD[`i'] " & " %5.2f fe_partisanship[`i'] " & " %5.2f fe_partisantrend[`i'] " & " %5.0f n_endorsements[`i'] " \\"
   }

  noisily display "\end{longtable}   "

  log close

}
///////////////////////////////////////////////////





**************************
*Appendix C
**************************
use "$mainPath/all_endorsements_JOP.dta", clear

*merge to ownership data, when available in the GSS panel data
merge m:1 member year using "$mainPath/GSPanelData.dta", gen(m_GS) keep(1 3)
replace ownerpermid = 0 if ownerpermid==.&year<=1994

*merge to county-level pres results of main county of circulation
merge m:1 countyfips year using "$mainPath/pres_county.dta", nogen keep(1 3)

tempfile endorsements_jop_merged
save `endorsements_jop_merged'

/*
Try 5 different versions of the bias estimates
o	"Simple" linear time trends (main text specification)
o	Allow newspaper bias to change each decade
o	"Rolling", by decade, measure of partisanship
o	add ownership and dem vote share to "simple" model 1
o	add ownership and dem vote share to "rolling" model 3
*/

*1) Linear Time Trends for each paper (original estimation)
use `endorsements_jop_merged', clear
bysort member: egen temp = count(member)
drop if temp<10
drop temp
reghdfe endorseD, absorb(member member#c.time election_g, savefe)
rename __hdfe1__ fe_partisanship_alt1
replace fe_partisanship_alt1 = fe_partisanship_alt1+_b[_cons]
replace fe_partisanship_alt1 = fe_partisanship_alt1+(time*__hdfe2__Slope1)
rename __hdfe2__Slope1 fe_partisantrend_alt1
rename __hdfe3__ fe_election_alt1
keep member year election_g fe_partisanship_alt1 fe_election_alt1
duplicates drop
tempfile altests1
save `altests1'

*2) By decade
use `endorsements_jop_merged', clear
local thresh = 10 // threshold for dropping newspapers with few endorsements
local twindow = 5 // + or - two election cycles (10 year period, 5 election cycles)
forvalues i = 1955(10)2015 {
	* calculate number of endorsements per newspaper during each time window
	bysort member: egen num_endorse = count(year) if year>=(`i'-`twindow')&year<(`i'+`twindow')
	reghdfe endorseD if year>=(`i'-`twindow')&year<(`i'+`twindow')&num_endorse>=`thresh', absorb(member election_g, savefe) resid
	gen tmp = _b[_cons] 
	gen fe_partisanship_alt2_`i' = __hdfe1__+tmp
	gen fe_election_alt2_`i' = __hdfe2__ 
	drop num_endorse tmp
}
egen fe_partisanship_alt2 = rowmean(fe_partisanship_alt2_*)
egen fe_election_alt2 = rowmean(fe_election_alt2_*)
keep member year election_g fe_partisanship_alt2 fe_election_alt2
duplicates drop
tempfile altests2
save `altests2'

*3) "Rolling" 
use `endorsements_jop_merged', clear
local thresh = 10 // threshold for dropping newspapers with few endorsements
local twindow = 4 // + or - two election cycles (10 year period, 5 election cycles)
gen fe_partisanship_alt3=.
forvalues i = 1950(2)2020 {
	local tperiod " year >= `i' - `twindow' & year < `i' + `twindow' "
	bysort member: egen num_endorse = count(year) if `tperiod'
	reghdfe endorseD if `tperiod' & num_endorse >= `thresh', absorb(member election_g, savefe) resid
	gen tmp = _b[_cons] 
	gen endorse_score_`i' = __hdfe1__+tmp
	replace fe_partisanship_alt3 = endorse_score_`i' if year==`i'
	gen election_fe_new_`i' = __hdfe2__ 
	bysort member: egen tmp2 = sum(endorseD) if `tperiod'&(endorseD==1|endorseD==0)
	gen endorse_dems_`i' = tmp2/num_endorse if `tperiod'&(endorseD==1|endorseD==0)
	drop num_endorse tmp tmp2
}
egen temp = rowmean(election_fe_new_*)
bysort election_g: egen fe_election_alt3 = mean(temp)
drop temp
gen endorse_dems = .
forval x=1950(2)2020{
	replace endorse_dems = endorse_dems_`x' if year==`x'	
}
drop endorse_dems_*
preserve
keep member year endorse_dems
duplicates drop 
duplicates tag member year, gen(tag)
drop if tag==1
drop tag
tempfile altests3_pt2
save `altests3_pt2'
restore 
keep member year election_g fe_partisanship_alt3 fe_election_alt3
duplicates drop
tempfile altests3
save `altests3'

*4) same as model 1, but add in owner and dem share as predictors
use `endorsements_jop_merged', clear
bysort member: egen temp = count(member)
drop if temp<10
drop temp
reghdfe endorseD d2pshare_pres, absorb(member member#c.time election_g ownerpermid, savefe)
rename __hdfe1__ fe_partisanship_alt4
replace fe_partisanship_alt4 = fe_partisanship_alt4+_b[_cons]
replace fe_partisanship_alt4 = fe_partisanship_alt4+(_b[d2pshare_pres]*d2pshare_pres)
replace fe_partisanship_alt4 = fe_partisanship_alt4+(time*__hdfe2__Slope1)
replace fe_partisanship_alt4 = fe_partisanship_alt4+__hdfe4__
rename __hdfe2__Slope1 partisantrend_alt4
rename __hdfe3__ fe_election_alt4
keep member year election_g fe_partisanship_alt4 fe_election_alt4
duplicates drop
tempfile altests4
save `altests4'

*5) "Rolling" measure that we use in evening newspapers, but add in owner and dem share as predictors
use `endorsements_jop_merged', clear
local thresh = 10 // threshold for dropping newspapers with few endorsements
local twindow = 4 // + or - two election cycles (10 year period, 5 election cycles)
gen fe_partisanship_alt5 = .
forvalues i = 1950(2)1994 {
	local tperiod " year >= `i' - `twindow' & year < `i' + `twindow' "
	bysort member: egen num_endorse = count(year) if `tperiod'
	reghdfe endorseD d2pshare_pres if `tperiod' & num_endorse >= `thresh', absorb(member election_g ownerpermid, savefe) resid
	gen endorse_score_`i' = __hdfe1__+_b[_cons] +(_b[d2pshare_pres]*d2pshare_pres)+__hdfe3__
	replace fe_partisanship_alt5 = endorse_score_`i' if year==`i'
	gen election_fe_new_`i' = __hdfe2__ 
	drop num_endorse 
}
egen temp = rowmean(election_fe_new_*)
bysort election_g: egen fe_election_alt5 = mean(temp)
drop temp
drop _reghdfe_resid __hdfe1__ __hdfe2__ __hdfe3__ election_fe_new_* endorse_score_*
keep member year election_g fe_partisanship_alt5 fe_election_alt5
duplicates drop
tempfile altests5
save `altests5'

*combine and compare estimates
use `altests1', clear
merge 1:1 member year election_g using `altests2', gen(alt2)
merge 1:1 member year election_g using `altests3', gen(alt3)
merge m:1 member year using `altests3_pt2', gen(alt3_pt2)
merge 1:1 member year election_g using `altests4', gen(alt4)
merge 1:1 member year election_g using `altests5', gen(alt5)

egen tag_election = tag(election_g)
egen tag_my = tag(member year)

foreach var of varlist fe_partisanship_alt*{
	replace `var' = `var'-0.5
	summ `var'
	replace `var' = `var'/r(sd)
}
foreach var of varlist fe_election_alt*{
	summ `var'
	replace `var' = `var'/r(sd)
}

///////////////////////////////////////////////////
**********
*Appendix Table C1
**********
corr fe_partisanship_alt1 fe_partisanship_alt1 if tag_my==1
corr fe_partisanship_alt1 fe_partisanship_alt2 if tag_my==1
corr fe_partisanship_alt1 fe_partisanship_alt3 if tag_my==1
corr fe_partisanship_alt1 fe_partisanship_alt4 if tag_my==1
corr fe_partisanship_alt1 fe_partisanship_alt5 if tag_my==1

corr fe_partisanship_alt2 fe_partisanship_alt2 if tag_my==1
corr fe_partisanship_alt2 fe_partisanship_alt3 if tag_my==1
corr fe_partisanship_alt2 fe_partisanship_alt4 if tag_my==1
corr fe_partisanship_alt2 fe_partisanship_alt5 if tag_my==1

corr fe_partisanship_alt3 fe_partisanship_alt3 if tag_my==1
corr fe_partisanship_alt3 fe_partisanship_alt4 if tag_my==1
corr fe_partisanship_alt3 fe_partisanship_alt5 if tag_my==1

corr fe_partisanship_alt4 fe_partisanship_alt4 if tag_my==1
corr fe_partisanship_alt4 fe_partisanship_alt5 if tag_my==1

corr fe_partisanship_alt5 fe_partisanship_alt5 if tag_my==1

corr fe_election_alt1 fe_election_alt1 if tag_election==1
corr fe_election_alt1 fe_election_alt2 if tag_election==1
corr fe_election_alt1 fe_election_alt3 if tag_election==1
corr fe_election_alt1 fe_election_alt4 if tag_election==1
corr fe_election_alt1 fe_election_alt5 if tag_election==1

corr fe_election_alt2 fe_election_alt2 if tag_election==1
corr fe_election_alt2 fe_election_alt3 if tag_election==1
corr fe_election_alt2 fe_election_alt4 if tag_election==1
corr fe_election_alt2 fe_election_alt5 if tag_election==1

corr fe_election_alt3 fe_election_alt3 if tag_election==1
corr fe_election_alt3 fe_election_alt4 if tag_election==1
corr fe_election_alt3 fe_election_alt5 if tag_election==1

corr fe_election_alt4 fe_election_alt4 if tag_election==1
corr fe_election_alt4 fe_election_alt5 if tag_election==1

corr fe_election_alt5 fe_election_alt5 if tag_election==1
///////////////////////////////////////////////////


*save copy of model 3 estimates for alternative results in the rest of appendix c
preserve
keep election_g fe_election_alt3
duplicates drop 
compress
save "$mainPath/quality_estimates_dynamic.dta", replace
restore
keep if tag_my==1 //keep unique member-year ("my") combos for analysis


///////////////////////////////////////////////////
**********
*Appendix Figure C1
**********
*Cincinnati Enquirer Example 
graph twoway (scatter fe_partisanship_alt1 year if member==134700, c(l)) (scatter fe_partisanship_alt2 year if member==134700, c(l)) (scatter fe_partisanship_alt3 year if member==134700, c(l)) (scatter fe_partisanship_alt4 year if member==134700, c(l)) (scatter fe_partisanship_alt5 year if member==134700, c(l)), ylab(-2(1)2) legend(on pos(6) rows(2) label(1 "Linear Trend") label(2 "By Decade") label(3 "Dynamic Bias") label(4 "Linear + Demand and Ownership") label(5 "Dynamic + Demand and Ownership")) ytitle("Endorsement-Based Partisanship (Pro-Dem Bias)") xtitle("Year") xlab(1950(4)2020, angle(30)) name(altmeasures3) yline(0, lcolor(black) lpattern(dash)) title("Cincinnati Enquirer") //cincinnita enquirer
graph export "$mainPath/figureC1.pdf", width(6) replace
rename fe_partisanship_alt3 newspaper_bias_dyn
///////////////////////////////////////////////////


*save model 3 dynamic newspaper bias estimates too
keep member year newspaper_bias_dyn endorse_dems
duplicates drop 
save "$mainPath/bias_estimates_dynamic.dta", replace


///////////////////////////////////////////////////
**********
*Appendix Table C2
**********
**LES scores (column 1)**
use "$mainPath/quality_estimates.dta", clear
merge 1:1 election_g using "$mainPath/quality_estimates_dynamic.dta", nogen keep(1 3)

keep if office=="H" 
merge 1:1 state_abbrev office year dist using "$mainPath/LES.dta", keep(3) nogen

*use model 3 quality estimates from above ('dynamic' ones), not original ones
replace quality_differential = fe_election_alt3
replace quality_differential=quality_differential*-1 if party=="R"

reg les quality_differential [aw=n_endorsements]
summ les if e(sample)
local depvarmean = r(mean)
local depvarstd = r(sd)
display `depvarmean'
display `depvarstd'
outreg2 using "$mainPath/tableC2.tex", tex(frag) label replace keep(quality_differential) addstat("Mean of Dependent Var", `depvarmean', "SD of Dependent Var", `depvarstd') adjr2 nocons dec(3) nonotes ctitle("~~~LES Scores~~~") addtext(Party-State FEs, No)


**approval ratings (column 2)**
use "$mainPath/quality_estimates.dta", clear
merge 1:1 election_g using "$mainPath/quality_estimates_dynamic.dta", nogen keep(1 3)

keep if office=="G"|office=="S"
drop dist 

*merge to election results
merge 1:m year state_abbrev office using "$mainPath/masterelections.dta", nogen keep(3)

*merge to approval ratings:
merge 1:1 year state_abbrev office using "$mainPath/JARSapprovals.dta", nogen keep(1 3)

*use model 3 quality estimates from above ('dynamic' ones), not original ones
replace quality_differential = fe_election_alt3
replace win_party="R" if win_party=="RF"
replace quality_differential = quality_differential*-1 if win_party=="R"

drop party
encode win_party, gen(party)

egen state_party = group(party state_fips)

label variable netpos "Net Approval"
label variable quality_differential "Quality Differential"

reghdfe netpos quality_differential [aw=n_endorsements], a(state_party)
summ netpos if e(sample)
local depvarmean = r(mean)
local depvarstd = r(sd)
display `depvarmean'
display `depvarstd'
outreg2 using "$mainPath/tableC2.tex", tex(frag) label keep(quality_differential) addstat("Mean of Dependent Var", `depvarmean', "SD of Dependent Var", `depvarstd') adjr2 nocons dec(3) nonotes ctitle("~~Net Approval~~") addtext(Party-State FEs, \checkmark)
///////////////////////////////////////////////////





///////////////////////////////////////////////////
**********
*Appendix Table C3
**********
use "$mainPath/quality_estimates.dta", clear
merge 1:1 election_g using "$mainPath/quality_estimates_dynamic.dta", nogen keep(1 3)

rename fe_election_alt3 quality_differential_alt3

*merge to election data 
merge 1:1 state_abbrev year office dist using "$mainPath/masterelections.dta",  keep(1 3)

*use model 3 quality estimates, not original ones
replace quality_differential = quality_differential_alt3

gen pdwin = .
replace pdwin = d2pshare>0.5 if d2pshare!=0&d2pshare!=1&d2pshare!=.

label variable quality_differential "Quality Differential"
label variable incumbency "Incumbency"

*dshare
reghdfe d2pshare quality_differential if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableC3.tex", tex(frag) label replace keep(quality_differential incumbency) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe d2pshare incumbency if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableC3.tex", tex(frag) label keep(quality_differential incumbency) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe d2pshare quality_differential incumbency if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableC3.tex", tex(frag) label keep(quality_differential incumbency) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

*pwin
reghdfe pdwin quality_differential if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableC3.tex", tex(frag) label keep(quality_differential incumbency) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe pdwin incumbency if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableC3.tex", tex(frag) label keep(quality_differential incumbency) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe pdwin quality_differential incumbency if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableC3.tex", tex(frag) label keep(quality_differential incumbency) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)
///////////////////////////////////////////////////






**************************
*Appendix D
**************************

///////////////////////////////////////////////////
**********
*Appendix Table D1
**********
*Estimate quality and bias using endorsements
use "$mainPath/all_endorsements_JOP.dta", clear

*quality and bias estimation
bysort member: egen temp = count(member)
drop if temp<10
drop temp
reghdfe endorseD, absorb(member member#c.time election_g, savefe)
keep if e(sample)
rename __hdfe1__ fe_partisanship
replace fe_partisanship = fe_partisanship+_b[_cons]
replace fe_partisanship = fe_partisanship+(time*__hdfe2__Slope1)
rename __hdfe2__Slope1 fe_partisantrend
rename __hdfe3__ fe_election

**Appendix Table D1 - column 1**
reghdfe endorseD, absorb(member member#c.time election_g, savefe)
local wr2 = (e(r2_a_within))
local wr2 : display %9.3f `wr2'
egen tag = tag(election_g)
summ tag if tag==1
local nelections = r(N)
drop tag
outreg2 using "$mainPath/tableD1.tex", tex(frag) label replace  adjr2 nocons dec(3) nonotes ctitle("Endorse D") addtext(Adjusted Within R^2, `wr2', Model, Fixed Effects, Newspaper FEs, \checkmark, Election FEs, \checkmark, N Elections, `nelections')


**Appendix Table D1 - columns 2 & 3**
use  "$mainPath/workingendorements_temp.dta", clear

label variable dist_diff2_cf "$(i_n-i_D)^2-(i_n-i_R)^2$"
label variable dist_diff2_dw "$(i_n-i_D)^2-(i_n-i_R)^2$"
label variable newspaper_in_cf "Newspaper Ideology"
label variable newspaper_in_dw "Newspaper Ideology"

**Appendix Table D1 - column 2**
rename dist_diff2_cf dist_diff2
rename newspaper_in_cf newspaper_in

reghdfe endorseD dist_diff2, absorb(election_g, savefe)
rename __hdfe1__ quality_differential_cf
egen tag = tag(election_g) if e(sample)
summ quality_differential_cf if tag==1, d
replace quality_differential_cf = quality_differential_cf/r(sd)
summ tag if tag==1
local nelections = r(N)
tab office if e(sample)&tag==1
drop tag
local wr2 = (e(r2_a_within))
local wr2 : display %9.3f `wr2'
outreg2 using "$mainPath/tableD1.tex", tex(frag) label keep(newspaper_in dist_diff2) adjr2 nocons dec(3) nonotes ctitle("Endorse D") addtext(Adjusted Within R^2, `wr2', Model, CF Adjusted, Newspaper FEs, No, Election FEs, \checkmark, N Elections, `nelections')


**Appendix Table D1 - column 3**
rename newspaper_in newspaper_in_cf
rename newspaper_in_dw newspaper_in

rename dist_diff2 dist_diff2_cf
rename dist_diff2_dw dist_diff2

reghdfe endorseD dist_diff2, absorb(election_g, savefe)
rename __hdfe1__ quality_differential_dw
*standardize, like the other measures
egen tag = tag(election_g) if e(sample)
summ quality_differential_dw if tag==1, d
replace quality_differential_dw = quality_differential_dw/r(sd)
summ tag if tag==1
local nelections = r(N)
tab office if e(sample)&tag==1
drop tag
local wr2 = (e(r2_a_within))
local wr2 : display %9.3f `wr2'
outreg2 using "$mainPath/tableD1.tex", tex(frag) label keep(newspaper_in dist_diff2) adjr2 nocons dec(3) nonotes ctitle("Endorse D") addtext(Adjusted Within R^2, `wr2', Model, DW-Nom Adjusted, Newspaper FEs, No, Election FEs, \checkmark, N Elections, `nelections')
rename dist_diff2 dist_diff2_dw
///////////////////////////////////////////////////



///////////////////////////////////////////////////
**********
*Appendix Table D2
**********

///////////////////////////////////////////////////

*get CF scores at election level, from endorsement dataset
use "$mainPath/all_endorsements_JOP.dta", clear
keep state_abbrev year office dist cfscoredyn_D cfscoredyn_R
duplicates drop
tempfile cf_scores
save `cf_scores'

*quality estimates (CF and DW adjusted versions)
use "$mainPath/quality_estimates_walternatives.dta", clear

*merge to election data 
merge 1:1 state_abbrev year office dist using "$mainPath/masterelections.dta",  keep(1 3)

gen pdwin = .
replace pdwin = d2pshare>0.5 if d2pshare!=0&d2pshare!=1&d2pshare!=.

*DIME CF scores at the election level:
merge m:1 state_abbrev year office dist using `cf_scores', gen(merge_dime_diffs) keep(1 3)

*flip all CF score - so that postive = more democratic, rather than the other way around:
foreach var of varlist cfscoredyn_D cfscoredyn_R{
	replace `var' = -1*`var'
}

*midpoint cf:
egen mdpt_cfdyn = rowmean(cfscoredyn_R cfscoredyn_D) if cfscoredyn_D > cfscoredyn_R & cfscoredyn_D < .

label variable quality_differential "Quality Differential"
label variable quality_differential_cf "Quality Differential"
label variable quality_differential_dw "Quality Differential"
label variable incumbency "Incumbency"
label variable mdpt_cfdyn "CF-Score Midpoint"

drop quality_differential


*CF
rename quality_differential_cf quality_differential

reghdfe d2pshare quality_differential incumbency mdpt_cfdyn if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
summ d2pshare if e(sample)
local depvarmean = r(mean)
local depvarstd = r(sd)
display `depvarmean'
display `depvarstd'
sum quality_differential if e(sample)
local indepvarmean = r(mean)
local indepvarstd = r(sd)
display `indepvarmean'
display `indepvarstd'
outreg2 using "$mainPath/tableD2.tex", tex(frag) label replace keep(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("CF-Adjusted-D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe pdwin quality_differential incumbency mdpt_cfdyn if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
summ pdwin if e(sample)
local depvarmean = r(mean)
local depvarstd = r(sd)
display `depvarmean'
display `depvarstd'
sum quality_differential if e(sample)
local indepvarmean = r(mean)
local indepvarstd = r(sd)
display `indepvarmean'
display `indepvarstd'
outreg2 using "$mainPath/tableD2.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("CF-Adjusted-P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)


*DW NOM
rename quality_differential quality_differential_cf
rename quality_differential_dw quality_differential

reghdfe d2pshare quality_differential incumbency mdpt_cfdyn if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
summ d2pshare if e(sample)
local depvarmean = r(mean)
local depvarstd = r(sd)
display `depvarmean'
display `depvarstd'
sum quality_differential if e(sample)
local indepvarmean = r(mean)
local indepvarstd = r(sd)
display `indepvarmean'
display `indepvarstd'
outreg2 using "$mainPath/tableD2.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("DWNOM-Adjusted-D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe pdwin quality_differential incumbency mdpt_cfdyn if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
summ pdwin if e(sample)
local depvarmean = r(mean)
local depvarstd = r(sd)
display `depvarmean'
display `depvarstd'
sum quality_differential if e(sample)
local indepvarmean = r(mean)
local indepvarstd = r(sd)
display `indepvarmean'
display `indepvarstd'
outreg2 using "$mainPath/tableD2.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("DWNOM-AdjustedP(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)













**************************
*Appendix E
**************************

/////////////////////////////////////////////////////
graph drop _all 
use "$mainPath/quality_estimates.dta", clear

*merge to election data 
merge 1:1 state_abbrev year office dist using "$mainPath/masterelections.dta",  keep(1 3)

gen pdwin = .
replace pdwin = d2pshare>0.5 if d2pshare!=0&d2pshare!=1&d2pshare!=.

*DIME CF scores at the election level:
merge m:1 state_abbrev year office dist using `cf_scores', gen(merge_dime_diffs) keep(1 3)

*flip all CF score - so that postive = more democratic, rather than the other way around:
foreach var of varlist cfscoredyn_D cfscoredyn_R{
	replace `var' = -1*`var'
}

*calculate midpoint
egen mdpt_cfdyn = rowmean(cfscoredyn_R cfscoredyn_D) if cfscoredyn_D > cfscoredyn_R & cfscoredyn_D!=.&cfscoredyn_R!=.
summ mdpt_cfdyn
replace mdpt_cfdyn = mdpt_cfdyn/r(sd)

label variable quality_differential "Quality Differential"
label variable incumbency "Incumbency"
label variable mdpt_cfdyn "CF-Score Midpoint"


**********
*Appendix Table E1
**********
reg d2pshare quality_differential if d2pshare!=0&d2pshare!=1
outreg2 using "$mainPath/tableE1.tex", tex(frag) label replace keep(quality_differential incumbency) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, No, Constituency FE, No)

reghdfe d2pshare quality_differential mdpt_cfdyn if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableE1.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe d2pshare incumbency mdpt_cfdyn if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableE1.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe d2pshare quality_differential incumbency mdpt_cfdyn if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableE1.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)


**********
*Appendix Table E2
**********
reg pdwin quality_differential if d2pshare!=0&d2pshare!=1
outreg2 using "$mainPath/tableE2.tex", tex(frag) label replace keep(quality_differential incumbency) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, No, Constituency FE, No)

reghdfe pdwin quality_differential mdpt_cfdyn if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableE2.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe pdwin incumbency mdpt_cfdyn if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableE2.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe pdwin quality_differential incumbency mdpt_cfdyn if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableE2.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)


**********
*Appendix Table E3
**********
label variable normalvote "Normal Vote"

reg d2pshare quality_differential if d2pshare!=0&d2pshare!=1
outreg2 using "$mainPath/tableE3.tex", tex(frag) label replace keep(quality_differential incumbency) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, No, District Normal Vote, No)

reghdfe d2pshare quality_differential mdpt_cfdyn normalvote if d2pshare!=0&d2pshare!=1, a(year)
outreg2 using "$mainPath/tableE3.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, District Normal Vote, \checkmark)

reghdfe d2pshare incumbency mdpt_cfdyn normalvote if d2pshare!=0&d2pshare!=1, a(year)
outreg2 using "$mainPath/tableE3.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, District Normal Vote, \checkmark)

reghdfe d2pshare quality_differential incumbency mdpt_cfdyn normalvote if d2pshare!=0&d2pshare!=1, a(year)
outreg2 using "$mainPath/tableE3.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, District Normal Vote, \checkmark)


**********
*Appendix Table E4
**********
reg pdwin quality_differential if d2pshare!=0&d2pshare!=1
outreg2 using "$mainPath/tableE4.tex", tex(frag) label replace keep(quality_differential incumbency) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, No, District Normal Vote, No)

reghdfe pdwin quality_differential mdpt_cfdyn normalvote if d2pshare!=0&d2pshare!=1, a(year)
outreg2 using "$mainPath/tableE4.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, District Normal Vote, \checkmark)

reghdfe pdwin incumbency mdpt_cfdyn normalvote if d2pshare!=0&d2pshare!=1, a(year)
outreg2 using "$mainPath/tableE4.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, District Normal Vote, \checkmark)

reghdfe pdwin quality_differential incumbency mdpt_cfdyn normalvote if d2pshare!=0&d2pshare!=1, a(year)
outreg2 using "$mainPath/tableE4.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn) sortvar(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, District Normal Vote, \checkmark)


**********
*Appendix Table E5
**********
merge 1:1 state_abbrev year office dist using "$mainPath/candidate_chars_differences.dta", nogen keep(1 3)

reghdfe d2pshare quality_differential incumbency mdpt_cfdyn if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableE5.tex", tex(frag) label replace keep(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe d2pshare quality_differential incumbency mdpt_cfdyn anyexp if d2pshare!=0&d2pshare!=1&year>=1980&(office=="H"|office=="S"|office=="PRESIDENT"|office=="ST H"|office=="ST S"), a(constituencyFE year)
outreg2 using "$mainPath/tableE5.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 D_spend_adv) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe d2pshare quality_differential incumbency mdpt_cfdyn  anyexp exp_decades exp_decades2 if d2pshare!=0&d2pshare!=1&year>=1980&(office=="H"|office=="S"|office=="PRESIDENT"|office=="ST H"|office=="ST S"), a(constituencyFE year)
outreg2 using "$mainPath/tableE5.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 D_spend_adv) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe d2pshare quality_differential incumbency mdpt_cfdyn  anyexp exp_decades exp_decades2 if d2pshare!=0&d2pshare!=1&year>=1980&(office=="H"|office=="S"|office=="PRESIDENT"|office=="ST H"|office=="ST S"), a(constituencyFE year)
reghdfe d2pshare quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 scandal if e(sample), a(constituencyFE year)
outreg2 using "$mainPath/tableE5.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 D_spend_adv scandal) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe d2pshare quality_differential incumbency mdpt_cfdyn  anyexp exp_decades exp_decades2 if d2pshare!=0&d2pshare!=1&year>=1980&(office=="H"|office=="S"|office=="PRESIDENT"|office=="ST H"|office=="ST S"), a(constituencyFE year)
reghdfe d2pshare quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 scandal D_spend_adv if e(sample), a(constituencyFE year)
outreg2 using "$mainPath/tableE5.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 scandal D_spend_adv) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)


**********
*Appendix Table E6
**********
reghdfe pdwin quality_differential incumbency mdpt_cfdyn if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/tableE6.tex", tex(frag) label replace keep(quality_differential incumbency mdpt_cfdyn) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe pdwin quality_differential incumbency mdpt_cfdyn anyexp if d2pshare!=0&d2pshare!=1&year>=1980&(office=="H"|office=="S"|office=="PRESIDENT"|office=="ST H"|office=="ST S"), a(constituencyFE year)
outreg2 using "$mainPath/tableE6.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 D_spend_adv) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe pdwin quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 if d2pshare!=0&d2pshare!=1&year>=1980&(office=="H"|office=="S"|office=="PRESIDENT"|office=="ST H"|office=="ST S"), a(constituencyFE year)
outreg2 using "$mainPath/tableE6.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 D_spend_adv) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe pdwin quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 if d2pshare!=0&d2pshare!=1&year>=1980&(office=="H"|office=="S"|office=="PRESIDENT"|office=="ST H"|office=="ST S"), a(constituencyFE year)
reghdfe pdwin quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 scandal if e(sample), a(constituencyFE year)
outreg2 using "$mainPath/tableE6.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 D_spend_adv scandal) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe pdwin quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 if d2pshare!=0&d2pshare!=1&year>=1980&(office=="H"|office=="S"|office=="PRESIDENT"|office=="ST H"|office=="ST S"), a(constituencyFE year)
reghdfe pdwin quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 scandal D_spend_adv if e(sample), a(constituencyFE year)
outreg2 using "$mainPath/tableE6.tex", tex(frag) label keep(quality_differential incumbency mdpt_cfdyn anyexp exp_decades exp_decades2 D_spend_adv scandal) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)
/////////////////////////////////////////////////////







/////////////////////////////////////////////////////
****************
*Final Results Datasets - Quality and Bias Estimates
****************

*Quality Estimates
use "$mainPath/quality_estimates_walternatives.dta", clear
merge 1:1 election_g using "$mainPath/quality_estimates_dynamic.dta", nogen keep(1 3)
order fe_election_alt3, after(quality_differential)
rename fe_election_alt3 quality_differential_dyn
sort state_abbrev year office dist
order state_abbrev year office dist election_g
label variable quality_differential "Quality Differential"
label variable quality_differential_dyn "Quality Differential Dynamic Bias"
label variable quality_differential_cf "CF-Adjusted Quality Differential"
label variable quality_differential_dw "DWNom-Adjusted Quality Differential"
compress
save "$mainPath/results/datasets/quality_estimates.dta", replace
export delimited "$mainPath/results/datasets/quality_estimates.csv", replace

*Bias Estimates
use "$mainPath/bias_estimates.dta", clear
merge 1:1 member year using "$mainPath/bias_estimates_dynamic.dta", nogen keep(1 3)
label variable newspaper_bias "Newspaper Bias - Linear"
label variable newspaper_bias_dyn "Newspaper Bias - Dynamic"
label variable endorse_dems "Dem Endorsed Share"
sort state_newspaper member year
order state_newspaper newspaper member year endorse_dem newspaper_bias newspaper_bias_dyn n_endorsements
rename state_newspaper state_abbrev
compress
save "$mainPath/results/datasets/bias_estimates.dta", replace
export delimited "$mainPath/results/datasets/bias_estimates.csv", replace
/////////////////////////////////////////////////////
